<template>
  <!-- 注册页面的整体盒子 -->
  <div class="reg-container">
    <!-- 注册的盒子 -->
    <div class="reg-box">
      <!-- 标题的盒子 -->
      <div class="title-box"></div>
      <!-- 注册的表单区域 -->
      <el-form class="reg" :model="formData" :rules="formRules" ref="formRef">
        <el-form-item prop="username">
          <el-input
            v-model="formData.username"
            prefix-icon="el-icon-user"
            placeholder="请输入用户名"
          ></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input
            v-model="formData.password"
            prefix-icon="el-icon-lock"
            placeholder="请输入密码"
          ></el-input>
        </el-form-item>
        <el-form-item prop="repassword">
          <el-input
            v-model="formData.repassword"
            prefix-icon="el-icon-unlock"
            placeholder="请输入确认密码"
          ></el-input>
        </el-form-item>
        <el-form-item>
          <el-button type="primary" style="width: 100%" @click="submitForm">
            注册</el-button
          >
          <el-link :underline="false">去登录</el-link>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  name: "Reg",
  data() {
    var samePWD = (rule, value, callback) => {
      if (value === "") {
        callback(new Error("请再次输入密码"));
      } else if (value !== this.ruleForm.password) {
        callback(new Error("两次输入密码不一致!"));
      } else {
        callback(); //放行
      }
    };
    return {
      //收集的表单数据
      formData: {
        username: "",
        password: "",
        repassword: "",
      },
      //表单的验证规则对象
      formRules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          {
            pattern: /^[0-9a-zA-Z]{1,10}$/,
            message: "请输入1~10的字母数字",
            trigger: "blur",
          },
        ],
        //校验密码
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          {
            pattern: /^\S{6,15}$/,
            message: "请输入6~15的非空字符串",
            trigger: "blur",
          },
        ],
        //再次确认密码
        repassword: [
          {
            pattern: /^\S{6,15}$/,
            message: "请输入6~15的非空字符串",
            trigger: "blur",
          },
          { validator: samePWD, trigger: "blur" },
        ],
      },
    };
  },
  methods: {
    submitForm() {
        //this.$refs.formRef 获取form组件，使用form组件调用validate(函数）自动校验表所有的表单项
      this.$refs.formRef.validate((valid) => {
        // valid:返回值true||false
        //   所有的校验通过了，返回才是true，那么只要有一项校验失败，返回的是false
        
        if (!valid)return //校验失败
        //注册成功
          const{data:res}=await this.$http.post('/api/reg',this.formData)
          console.log('res',res);
          if (res.code===0) {
            this.$message.success(res.message)
            //跳转页面登陆页 this.$router编程式导航
            this.$router.push('/login')
          } else {
            this.message.error(res.message);
          } 
      });
    },
  },
};
</script>

<style lang="less" scoped>
.reg-container {
  background: url("../../assets/images/login_bg.jpg") center;
  background-size: cover;
  height: 100%;

  .reg-box {
    width: 400px;
    height: 335px;
    background-color: #fff;
    border-radius: 3px;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -50%);

    .reg {
      padding-left: 25px;
      padding-right: 25px;
    }

    .title-box {
      height: 60px;
      background: url("../../assets/images/login_title.png") center no-repeat;
    }
  }
}
</style>
